/*
 * @Author: gk 
 *  系统初始化
 * @Date: 2018-03-15 14:44:57 
 * @Last Modified by: csy
 * @Last Modified time: 2019-08-30 11:09:19
 */
layui.config({
}).use(['element', 'zlContext', 'zlConfig'], function (exports) {
    var $ = layui.$
        , zlContext = layui.zlContext
        , form = layui.form
        , element = layui.element
        , zlConfig = layui.zlConfig
        , statusName = zlConfig.global.result.statusName
        , msgName = zlConfig.global.result.msgName
        , dataName = zlConfig.global.result.dataName
        , ERROR = zlConfig.global.result.errorState
        , OK = zlConfig.global.result.okState;
    $(window).on('resize', function () {
        $('.docking-left').height($(window).height() - 10);
        $('.docking-fill').height($(window).height() - 10);
    }).resize();


    zlContext.post(layui.cache['contentPath'] + '/manager/init/getconfigs', {}, function (result) {
        if (result[statusName] == OK) {
            var configs = result[dataName];
            $('#frmGridInit input[name=url]').val(configs['spring.datasource.url']);
            $('#frmGridInit input[name=username]').val(configs['spring.datasource.username']);
            $('#frmGridInit select[name=driver-class-name]').val(configs['spring.datasource.driver-class-name']);
            form.render();
        }
        else {
            zlContext.errorMsg(result[msgName]);
        }
    });
    form.on('select(dbtype)', function (data) {
        $('#frmGridInit input[name=url]').val("");
        $('#frmGridInit input[name=username]').val("");
    });
    $('#btnStrtTest').on('click', function () {
        var param = {
            driver: $('#frmGridInit select[name=driver-class-name]').val()
            , url: $('#frmGridInit input[name=url]').val()
            , username: $('#frmGridInit input[name=username]').val()
        };
        var loadingIndex = zlContext.showLoading();
        zlContext.post(layui.cache['contentPath'] + '/manager/init/testconnect', param, function (result) {
            zlContext.closeLoading(loadingIndex);
            if (result[statusName] == OK) {
                zlContext.successMsg('链接成功');
            }
            else {
                zlContext.errorMsg(result[msgName]);
            }
        });
    });
    var isInit = false;
    $('#btnStartInit').on('click', function () {

        zlContext.confirm('询问', '是否要开始初始化操作? \n【注意】执行初始化会删除原来的表数据.', function (index) {
            var param = {
                driver: $('#frmGridInit select[name=driver-class-name]').val()
                , url: $('#frmGridInit input[name=url]').val()
                , username: $('#frmGridInit input[name=username]').val()
                , password: $('#frmGridInit input[name=password]').val()
            };
            $('#processTxt').empty();
            isInit = true;
            var loadingIndex = zlContext.showLoading();
            zlContext.post(layui.cache['contentPath'] + '/manager/init/start', param, function (result) {
                zlContext.closeLoading(loadingIndex);
                isInit = false;

                if (result[statusName] == OK) {
                    element.progress('processBar', '100%');
                    zlContext.successMsg('初始化完成');
                }
                else {
                    if ("-9999" == result[msgName] && result[dataName]) {
                        //数据库中存在表
                        var tbNames = result[dataName];
                        showTableNames(tbNames);
                    } else {
                        zlContext.errorMsg(result[msgName]);
                    }
                }

            });
            layer.close(index);
        });
    });
    $('#btnGotoHome').on('click', function () {
        window.location.href = layui.cache['contentPath'] + '/manager/1001';
    });

    setInterval(function () {
        queryProcess();
    }, 2000);
    //上一个请求没完就不要干事
    var isPrvFinish = true;
    function queryProcess() {
        if (!isPrvFinish) { return; }
        isPrvFinish = false;
        zlContext.get(layui.cache['contentPath'] + '/manager/init/getinitprocess', {}, function (result) {
            if (result[statusName] == OK) {
                $(result[dataName]).each(function (index, item) {
                    var obj = eval('(' + item + ')')
                    if (obj.msg) {
                        $('#processTxt').append($('<p>' + obj.msg + '</p>'));
                    }
                    if (obj.process) {
                        element.progress('processBar', Math.floor((obj.process / obj.total) * 100) + '%');
                    }
                });
            }
            else {
                zlContext.errorMsg(result[msgName]);
            }
            isPrvFinish = true;
        }, false);
    }

    //展示已存在的表名
    function showTableNames(tbNames) {
        //弹出信息框
        var content = [
            '<div class="layui-card">'
            , ' <div class="layui-card-header">数据库中存在以下数据表，请手动删除后重新初始化</div>'
            , ' <div class="layui-card-body" id="tb-name-container">'
            , ' </div>'
            , '</div>'
        ];
        layer.open({
            type: 1
            , title: "平台初始化失败" //不显示标题栏
            , closeBtn: false
            , area: ['350px', '380px']
            , id: 'layer_tb_name' //设定一个id，防止重复弹出
            , btn: '确认关闭'
            , moveType: 1 //拖拽模式，0或者1
            , content: content.join('')
            , success: function (layero, index) {
                var $container = layero.find('#tb-name-container');
                $.each(tbNames, function (i, item) {
                    $container.append("<span>" + item + "</span><br>");
                });
            },
            yes: function (index, layero) {
                layer.close(index);
                return false;
            }
        });
    }
});